<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/25
 * Time: 15:03
 */
namespace  app\admin\controller;
use think\Controller;
use think\Db;
class Jurisdiction extends Controller
{
    private $AdminRoleState='';//是否为超级管理员状态
    private $AuthList='';//所有有效的栏目ID
    private $AuthListInfo;//所有栏目ID对应的数据
    private $AuthOneID='';//获取当前URL连接的一级栏目ID
	private $PageCount=15;
    public function _initialize()
    {
		$this->PageCount=20;
        $this->LoginCheck();
        //基于当前Url连接中的 项目组 控制器和方法，获取其一级ID
        //$this->GetCurrentUrl();
        //$Rules=$this->GetJurisdictionList();//获取权限列表
       // $this->assign('RulesList',$Rules);
    }
    //登录时时验证
    private function LoginCheck()
    {
        if (!session('sid')) {
            $this->redirect('login/index');
        }
		// 执行用户登录
        $where = array(
            'username' => session('username')
        );
		 // 执行登录
        $result = Db::name('user')->field('id,ucode,username,realname')
            ->where($where)
            ->where('status=1')
            ->find();
        if(empty($result)){
            $this->alert('您的账户登录异常或在其他地方有所登录，请重新登录','jump',url('login/index'));
            exit;
        }
    }

	// 文件上传方法
    public function upload()
    {
        $upload = upload('upload');
        if (is_array($upload)) {
            json(1, $upload);
        } else {
            json(0, $upload);
        }
    }


	// 检查是否在黑名单
    private function checkLoginBlack()
    {
        // 读取黑名单
        $ip_black = ROOT_PATH . '/data/' . md5('login_black') . '.php';
        if (file_exists($ip_black)) {
            $data = require $ip_black;
            $user_ip = get_user_ip();
            $lock_time = $this->config('lock_time') ?: 900;
            $lock_count = $this->config('lock_count') ?: 5;
            if (isset($data[$user_ip]) && $data[$user_ip]['count'] >= $lock_count && time() - $data[$user_ip]['time'] < $lock_time) {
                return $lock_time - (time() - $data[$user_ip]['time']); // 返回剩余秒数
            }
        }
        return false;
    }

    // 添加登录黑名单
    private function setLoginBlack()
    {
        // 读取黑名单
        $ip_black = ROOT_PATH . '/data/' . md5('login_black') . '.php';
        if (file_exists($ip_black)) {
            $data = require $ip_black;
        } else {
            $data = array();
        }
        
        // 添加IP
        $user_ip = get_user_ip();
        $lock_time = $this->config('lock_time') ?: 900;
        $lock_count = $this->config('lock_count') ?: 5;
        if (isset($data[$user_ip]) && $data[$user_ip]['count'] < $lock_count && time() - $data[$user_ip]['time'] < $lock_time) {
            $data[$user_ip] = array(
                'time' => time(),
                'count' => $data[get_user_ip()]['count'] + 1
            );
        } else {
            $data[$user_ip] = array(
                'time' => time(),
                'count' => 1
            );
        }
        
        // 写入黑名单
        check_file($ip_black, true);
        return file_put_contents($ip_black, "<?php\nreturn " . var_export($data, true) . ";");
    }
    
}